<?php

namespace App;

use DB;
use Illuminate\Database\Eloquent\Model;
use Storage;

/**
 * Class Banner
 *
 * @package App
 * @mixin \Eloquent
 */
class Ztbanner extends Model
{
    public $timestamps = false;
    protected $table = 'kl_kind_banner';


    //删除
    const DEL_NO = 0;//删除了
    const DEL_OFF = 1; //未删除

    static $DelDisplay = [
        self::DEL_NO => '不显示',
        self::DEL_OFF => '显示'
    ];

    //内部跳转位置
    const TYPE_WEB = 1;
    const TYPE_APP = 2;
	const TYPE_SHANG = 3;
	const TYPE_GAME = 4;
	const TYPE_AD = 5;
	
    static $TypeDisplay = [
        self::TYPE_WEB => 'web',
        self::TYPE_APP => '帖子',
        self::TYPE_SHANG => '商家',
        self::TYPE_GAME => '游戏乐园',		
		self::TYPE_AD => '广告',
    ];

    const JUMP_YES = 1; 
    const JUMP_NO = 2;
    static $JUMPDisplay = [
        self::JUMP_YES => '跳转',
        self::JUMP_NO => '不跳转'
    ];
    /**
     * 添加banner
     * @param $request
     * @return array
     */
    static function ZtfenleibnPublish($request)
    {
        $banner = new Ztbanner();
        //banner
        if ($request->has('pic_url')) {
            $path = Storage::url(Postings::OssUpdate($request->pic_url));
            $banner->pic_url = $path;
        } else {
            return ['status' => 2, 'message' => '图片呢?'];
        }
        /*if (intval($request->is_jump) === self::JUMP_NO) {
            $banner->is_jump = self::JUMP_NO;
        } else {
            $banner->link = $request->link;
        }
		if(intval($request->type) === self::TYPE_WEB){
			if (!is_numeric($request->link_id)) {
				return ['status' => 2, 'message' => 'ID是数字哦'];
			}
			$banner->link_id = $request->link_id;
		}*/
		if (intval($request->is_jump) === self::JUMP_NO) {
            $banner->is_jump = self::JUMP_NO;
        } 
		if($request->link){
			$banner->link = $request->link;
		}else{
			$banner->link = '';
		}	
	
		if($request->kind_id){
			$banner->kind_id = $request->kind_id;
		}
		if($request->type){
			$banner->type = $request->type;
		}
        if (!$banner->save()) {
            return ['status' => 2, 'message' => '添加失败'];
        }

        return ['status' => 1, 'message' => '添加成功'];
    }


    /**
     * 修改banner
     * @param $request
     * @param $banner
     * @return array
     */
    static function ZtfenlieibnModify($request, $banner)
    {

        //banner
        if ($request->has('pic_url')) {
            $path = Storage::url(Postings::OssUpdate($request->pic_url));
            $banner->pic_url = $path;
        }

       /* if (intval($request->is_jump) === self::JUMP_NO) {
            $banner->is_jump = self::JUMP_NO;
        } else {
            $banner->is_jump = self::JUMP_YES;
            $banner->link = $request->link;
        }

		if(intval($request->type) === self::TYPE_APP || intval($request->type) === self::TYPE_AD){
			if (!is_numeric($request->link_id)) {
				return ['status' => 2, 'message' => 'ID是数字哦'];
			}
			$banner->link_id = $request->link_id;
		}
        $banner->type = $request->type;*/
		if (intval($request->is_jump) === self::JUMP_NO) {
            $banner->is_jump = self::JUMP_NO;
        } 
		if($request->link){
			$banner->link = $request->link;
		}else{
			$banner->link = '';
		}	
	
		if($request->kind_id){
			$banner->kind_id = $request->kind_id;
		}
		if($request->type){
			$banner->type = $request->type;
		}
        if (!$banner->save()) {
            return ['status' => 2, 'message' => '修改失败'];
        }

        return ['status' => 1, 'message' => '修改成功'];
    }
	
	//删除banner
    static function Del($id, $request)
    {
        DB::beginTransaction();

        if (!Ztbanner::where('id', $id)->update([
            'is_del' => Ztbanner::DEL_NO
        ])) {
            DB::rollBack();

            return ['status' => 2, 'message' => '删除失败'];
        }

        DB::commit();

        return ['status' => 1, 'message' => '删除成功'];
    }

}
